package io.gitee.kotle.android.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.shape.*
import io.gitee.kotle.android.R
import io.gitee.kotle.android.widget.helper.handleCustomViewBackground


/**
 * <style name="StyleShapeAppearanceImage" parent="">
 * <item name="cornerFamily">rounded</item>
 * <item name="cornerSize">16dp</item>
 * <item name="cornerSizeTopRight">10dp</item>
 * <item name="cornerSizeBottomRight">0dp</item>
 * </style>
 */
open class BaseShapeableImageView : ShapeableImageView {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        attrs?.let {
            context.obtainStyledAttributes(attrs, R.styleable.BaseShapeableImageView).apply {
                val isCircle = getBoolean(R.styleable.BaseShapeableImageView_isCircle, false)
                if (isCircle) {
                    setCircleImage()
                } else {
                    val roundRadius =
                        getDimension(R.styleable.BaseShapeableImageView_roundRadius, -1f)
                    if (roundRadius >= 0) {
                        setRoundRadius(roundRadius)
                    }
                }
                handleAttr(this)
                this.recycle()
            }
        }
    }

    private fun handleAttr(attributes: TypedArray) {
        //处理背景色
        handleCustomViewBackground(
            attributes,
            _rippleColor = R.styleable.BaseShapeableImageView_rippleColor,
            _unboundedRipple = R.styleable.BaseShapeableImageView_unboundedRipple,
            _backgroundNormal = R.styleable.BaseShapeableImageView_backgroundNormal,
            _backgroundPressed = R.styleable.BaseShapeableImageView_backgroundPressed,
            _backgroundUnEnable = R.styleable.BaseShapeableImageView_backgroundUnEnable,
            _cornerRadius = R.styleable.BaseShapeableImageView_cornerRadius,
            _cornerSizeTopLeft = R.styleable.BaseShapeableImageView_cornerSizeTopLeft,
            _cornerSizeTopRight = R.styleable.BaseShapeableImageView_cornerSizeTopRight,
            _cornerSizeBottomLeft = R.styleable.BaseShapeableImageView_cornerSizeBottomLeft,
            _cornerSizeBottomRight = R.styleable.BaseShapeableImageView_cornerSizeBottomRight,
            _strokeColor = R.styleable.BaseShapeableImageView_strokeColor,
            _strokeWidth = R.styleable.BaseShapeableImageView_strokeWidth,
            _backgroundShape = R.styleable.BaseShapeableImageView_backgroundShape,
            _backgroundAlpha = R.styleable.BaseShapeableImageView_backgroundAlpha,
        )
    }

/*    fun set() {
        shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setAllCorners(CornerFamily.ROUNDED, 20f)
                .setTopLeftCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))
                .setTopRightCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))
                .setBottomRightCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))
                .setBottomLeftCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))
                .setAllCornerSizes(ShapeAppearanceModel.PILL)
                .setTopLeftCornerSize(20f)
                .setTopRightCornerSize(RelativeCornerSize(0.5f))
                .setBottomLeftCornerSize(10f)
                .setBottomRightCornerSize(AbsoluteCornerSize(30f))
                .build()
    }*/
    /**
     * 设置为圆形图片
     */
    fun setCircleImage() {
        shapeAppearanceModel = shapeAppearanceModel.toBuilder()
            .setAllCornerSizes(RelativeCornerSize(0.5f))
            .build()
    }

    /**
     * 设置图片圆角
     */
    fun setRoundRadius(radius: Float) {
        shapeAppearanceModel = shapeAppearanceModel.toBuilder()
            .setAllCornerSizes(AbsoluteCornerSize(radius))
            .build()
    }
}